//
//  OSSViewController.swift
//  TTProduct-macOS
//
//  Created by Toj on 4/15/21.
//

import Cocoa

class OSSViewController: TTViewController {
    private lazy var textField: NSTextField = {
        let view = NSTextField()
        view.backgroundColor = .red
        return view
    }()
    
    private lazy var button: NSButton = {
        let view = NSButton()
        return view
    }()

    var oss: TTOSS?
    var aResource: OSSResource?
    override func viewDidLoad() {
        super.viewDidLoad()

        let resource = OSSResource()
        aResource = resource
        //resource.serverUrl = "https://portal.vipcode.com/api/common/v1.0/getSecurityToken"

        resource.endpoint = "oss-cn-beijing.aliyuncs.com"
        
        resource.accessKey = "LTAIHghSOQkNxDlA"
        resource.secretKey = "Twy7f7FoAFKmU1E26IuryrJg3j2bH5"
        resource.bucketName = "vipcode-client"
        
        resource.keyPath = "log/test/txt.txt"
        // resource.ossFileName = "vcEditer.zip"
        resource.token = ""
        resource.expiration = ""

        resource.fromURL = ""
        resource.toURL = ""

        view.addSubview(button)
        view.addSubview(textField)
        button.snp.makeConstraints { (make) in
            make.bottom.equalTo(view.snp.bottom).offset(-100)
            make.left.height.width.equalTo(100)
        }
        
        textField.snp.makeConstraints { (make) in
            make.left.centerY.right.equalTo(view)
            make.height.equalTo(20)
        }

        button.target = self
        button.action = #selector(self.didTapButton)
        button.wantsLayer = true
        button.layer?.backgroundColor = NSColor.red.cgColor
    }

    @objc private func didTapButton() {
        let fileURL = "/Users/vipcode/Desktop/TESTFile/R"
        let filePath = "/Users/vipcode/Desktop/TESTFile/txt.txt"
        let url = textField.stringValue
        var isDirectory = ObjCBool(false)
       let isExists = FileManager.default.fileExists(atPath: url, isDirectory: &isDirectory)

        print("isExists:\(isExists)")
        print("isDirectory:\(isDirectory.boolValue)")
        
        return
        print(className + " : " + #function)
        guard let resource = aResource else {
            return
        }
       // let from = "/Users/vipcode/Desktop/vcEditer.zip"
        let to = "/Users/vipcode/Desktop/TESTFile/TOK.txt"

        
        oss = TTOSS(resource: resource)
//        oss?.upload(from: from) { (a, b, c) in
//            print(a)
//            print(b)
//            print(c)
//        } completed: { (isSuccess, info) in
//            print(isSuccess)
//            print(info)
//        }
        oss?.download(to: to, progress: { (write, totalWrite, total) in
            print(write)
            print(totalWrite)
            print(total)
        }, completed: { (isSuccess, data, info) in
            print(isSuccess)
            print(data?.count ?? 0)
            print(info)
        })
    }
}
